/**
 * Author: TanShenghu
 * Email: tanshenghu@163.com
 * Date: 2015-04-12
 * Des: 对javascript原生方法的一些扩展
 * Git: http://git.oschina.net/tanshenghu/widget/
 */
var Util = require( 'util' );

String.prototype.rtrim = function( delStr ){
    delStr = delStr || this.slice( -1 );
    var reg = new RegExp( '(?=.)(?:'+delStr+')+(?:[^'+delStr+']*)$', 'mg' );
    return this.replace(reg, function(a,b){
        return a.replace( new RegExp( delStr, 'mg' ), '');
    });
};
String.prototype.ltrim = function( delStr ){
    delStr = delStr || this.slice( 0, 1 );
    var reg = new RegExp( '^(?:[^'+delStr+']*)('+delStr+')+', 'mg' );
    return this.replace(reg, function(a,b){
        return a.replace( new RegExp( delStr, 'mg' ), '');
    });
};
String.prototype.colorHex = function( ){
    var result,val;
    result = val = this;
    if( /^#{1}[a-fA-F0-9]{3,6}$/.test( val ) ){
        result = '';
        if( val.length===4 ){
            val = val.replace(/[^#]/g, function(a){
                return a+a;
            })
        }
        val.match( /[^#]{2}/g ).forEach(function(item){
            result += ',' + parseInt( item, 16 );
        })
        result = result.slice( 1 );
        
    }else if( /^\d{1,3},\d{1,3},\d{1,3}$/.test( val ) ){
        result = '#';
        val.split(',').forEach(function(item){
            result += parseInt(item).toString( 16 ).length<2?0+parseInt(item).toString( 16 ):parseInt(item).toString( 16 );
        })
    }
    return result;
    
};
Math.rnd = function( s, e, int ){
    var i, result;
    if( e < s ){
        i = s;s = e;e = i;
    }
    result = Math.random() * ( e-s ) + s;
    if( int==true ){
        result = Math.min( e, Math.round( result ) );
    }
    return result;
};
Array.prototype.random = function(){
    return this[ Math.rnd( 0, this.length, true ) ];
};
Array.prototype.removeItem = function( val, repeat ){
    var one = false, result = [];
    for(var i in this){
        if ( this[i]===val && !one ){
            result.push( this.splice(i, 1) );
            if ( !repeat ){
                one = true;
            }
        }
    }
    return result;
};
Array.prototype.removeRepeat = function(){
    var item = {}, result = [];
    for(var i=0,l=this.length; i<l; i++){
        var m = (typeof this[i])+this[i];
        if ( item[m] === undefined ){
            result.push( this[i] );
            item[m] = 'yes';
        }
    }
    
    return result;
};
Date.prototype.format = function(format){
    var o = {
        "m+" : this.getMonth()+1,
        "d+" : this.getDate(),
        "h+" : this.getHours(),
        "i+" : this.getMinutes(),
        "s+" : this.getSeconds(),
        "q+" : Math.floor((this.getMonth()+3)/3),
        "S" : this.getMilliseconds()
    }
    if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
    (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)if(new RegExp("("+ k +")").test(format))
    format = format.replace(RegExp.$1,
    RegExp.$1.length==1 ? o[k] :
    ("00"+ o[k]).substr((""+ o[k]).length));
    return format;
};
Util.isMap = function( o ){
    return {}.toString.call( o )==='[object Object]';
}